package com.itheima.lambda;

import com.itheima.innerclass.Student;

import java.util.Arrays;
import java.util.Comparator;

public class LambdaDemo2 {
    public static void main(String[] args) {
        //定义学生类
        Student[] stu = new Student[6];
        stu[0] = new Student("张三", 18, 100);
        stu[1] = new Student("李四", 19, 90);
        stu[2] = new Student("王五", 20, 80);
        stu[3] = new Student("赵六", 21, 70);
        stu[4] = new Student("孙七", 22, 60);
        stu[5] = new Student("钱八", 23, 50);
        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i]);
        }
        System.out.println("------------------------------------------------------");

        //常规匿名内部类的使用
        Arrays.sort(stu, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        //使用Lambda表达式
        Arrays.sort(stu, (Student o1, Student o2) -> {
            return o1.getAge() - o2.getAge();
        });

        //优化Lambda表达式
        Arrays.sort(stu, (o1, o2) -> o1.getAge() - o2.getAge());

        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i]);
        }

    }
}
